package com.fate.service.impl;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.domain.SysUserRole;
import com.fate.mapper.SysRoleMapper;
import com.fate.mapper.SysUserRoleMapper;
import com.fate.service.SysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fate.mapper.SysUserMapper;
import com.fate.domain.SysUser;
import com.fate.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author 武汉尚学堂
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询后台管理员的接口
     * 分页查询不需要放缓存的
     *
     * @param page
     * @param sysUser
     * @return
     */
    @Override
    public Page<SysUser> findSysUserByPage(Page<SysUser> page, SysUser sysUser) {
        // 分页查询 根据创建时间排序
        page.addOrder(OrderItem.desc("create_time"));
        return (Page<SysUser>) sysUserMapper.selectPage(page, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(sysUser.getUsername()), SysUser::getUsername, sysUser.getUsername())
        );
    }

    /**
     * 新增的方法
     * 1.密码还没做
     * 增删改查一定要打日志
     * 新增就是需要设置一些默认值
     * 新增用户 需要往角色和用户中间表插入数据
     *
     * @param sysUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean save(SysUser sysUser) {
        log.info("新增管理员,操作员id:{}",sysUser.getCreateUserId());
        sysUser.setCreateTime(new DateTime());
        sysUser.setShopId(1L);
        //处理密码 加密 密文
        String password = sysUser.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        sysUser.setPassword(encode);
        int insert = sysUserMapper.insert(sysUser);
        if (insert>0){
            //操作角色用户表
            Long userId = sysUser.getUserId();
            List<Long> roleIdList = sysUser.getRoleIdList();
            //循环创建对象 插入数据 不要再循环中操作数据库
            List<SysUserRole> sysUserRoles = new ArrayList<>(roleIdList.size() * 2);
            roleIdList.forEach(roleId->{
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId);
                sysUserRoles.add(sysUserRole);
            });
            //1. xml foreach 2 saveBatch
            //操作数据库的时候提交事务 连接的操作是很耗费性能 指定每次提交的数量可以提高mysql 的吞吐量
        sysUserRoleService.saveBatch(sysUserRoles);

        }
        return insert>0;
    }
}
